package com.sugar.controller;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author felix
 * @date 2024/11/15 15:28
 */
@RestController
@RequestMapping("/global")
public class AdviceController {

    /** 获取全局绑定数据 **/

    @GetMapping("/param1")
    public String getGlobalParam1(Model model){
        Map<String, Object> modelMap = model.asMap();
        return (String) modelMap.get("globalAttr");
    }

    @GetMapping("/param2")
    public String getGlobalParam2(@ModelAttribute("globalAttr") String globalAttr){
        return globalAttr;
    }

    @GetMapping("/param3")
    public String getGlobalParam3(ModelMap modelMap){
        return (String)modelMap.get("globalAttr");
    }

    @GetMapping("/param4")
    public Map getGlobalParam4(Model model){
        Map<String, Object> modelMap = model.asMap();
        Map<String,String> map  = (Map) modelMap.get("map");
        return map;
    }

    @GetMapping("/param5")
    public String getGlobalParam5(@ModelAttribute("map") Map<String,String> map){
        return  map.get("key1");
    }

    @GetMapping("/param6")
    public Map getGlobalParam6(ModelMap modelMap){
        return (Map) modelMap.get("map");
    }

    @GetMapping("/param7")
    public Map getGlobalParam7(ModelMap modelMap){
        return (Map) modelMap.get("myMap");
    }
}
